﻿using Alos.IRepository;
using Alos.IService;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Alos.Entity;
using Alos.Common;
using System.Threading.Tasks;

namespace Alos.Service
{
    public class ReResourceService : BaseService<ReResource>, IReResourceService
    {
        IReResourceRepository ReResourceRepository;
        IReTestRepository ReTestRepository;
        IResourceWhiteboardRepository ResourceWhiteboardRepository;
        IReComTestRepository ReComTestRepository;
        IReTaskRepository ReTaskRepository;
        public ReResourceService(IReResourceRepository _ReResourceRepository,
                                 IReTestRepository _ReTestRepository,
                                 IResourceWhiteboardRepository _ResourceWhiteboardRepository,
                                 IReComTestRepository _ReComTestRepository,
                                 IReTaskRepository _ReTaskRepository)
        {
            ReResourceRepository = _ReResourceRepository;
            ReTestRepository = _ReTestRepository;
            ResourceWhiteboardRepository = _ResourceWhiteboardRepository;
            ReComTestRepository = _ReComTestRepository;
            ReTaskRepository = _ReTaskRepository;
        }
        public async Task<List<dynamic>> GetReResourceById(string ids)
        {
            if (!ids.IsSpeaterBy(","))
                return null;

            string cacheKey = "resouce-" + ids;

            var data = (dynamic)Common.DataCache.GetCache(cacheKey);
            if (data == null)
            {
                var ResInfo = ReResourceRepository.GetReResourceById(ids.ToString()).ToList();
                if (ResInfo != null && ResInfo.Count > 0)
                {
                    foreach (var res in ResInfo)
                    {
                        switch (res.ReTypeCode as string)
                        {
                            case "RE0001": res.child = await Task.Factory.StartNew(mid => { return getChildReResourceByResId((int)mid); }, (int)res.ID); break; //课件
                            case "RE0002": res.child = await Task.Factory.StartNew(mid => { return getTestCombination((int)mid); }, (int)res.ID); break; //试卷
                            case "RE0003": res.child = await Task.Factory.StartNew(mid => { return getTask((int)mid); }, (int)res.ID); break;  //自学任务单
                            case "RE0004": ; break; //研讨任务单
                            case "RE0005": ; break; //微课
                            case "RE0006": ; res.child = await Task.Factory.StartNew(mid => { return getWhiteboard((int)mid).ToList(); }, (int)res.ID); break; //互动题板
                            case "RE0007": res.child = await Task.Factory.StartNew(mid => { return getChildReResourceByResId((int)mid); }, (int)res.ID); break; //教案
                            case "RE0008": res.child = await Task.Factory.StartNew(mid => { return getChildReResourceByResId((int)mid); }, (int)res.ID); break; //导学案
                            case "RE0009": res.child = await Task.Factory.StartNew(mid => { return getTestCombination((int)mid); }, (int)res.ID); break; //课堂测验
                            case "RE0010": res.child = await Task.Factory.StartNew(mid => { return getTestCombination((int)mid); }, (int)res.ID); break; //作业
                            case "RE0011": res.child = await Task.Factory.StartNew(mid => { return getTest((int)mid); }, (int)res.ID); break; //试题
                            case "RE0012": ; break; //课前诊断单
                            case "RE0013": res.child = await Task.Factory.StartNew(mid => { return getChildReResourceByResId((int)mid); }, (int)res.ID); break; //素材
                            case "RE0014": res.child = await Task.Factory.StartNew(mid => { return getTestCombination((int)mid); }, (int)res.ID); break; //下载试题组合
                            default: res.child = await Task.Factory.StartNew(mid => { return getChildReResourceByResId((int)mid); }, (int)res.ID); break; //素材; break;
                        }
                    }
                }
                Common.DataCache.SetCache(cacheKey, ResInfo, null, TimeSpan.FromSeconds(100));
                data = ResInfo;
            }
            return data;
        }
        #region

        /// <summary>
        /// 获取子资源（如文档转换后的图片等）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private List<dynamic> getChildReResourceByResId(int id)
        {
            return ReResourceRepository.GetReResourceByFatherID(id).ToList();
        }
        /// <summary>
        /// 获取包括 组合 试卷在内的试题集合
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private dynamic getTestCombination(int id)
        {
            return ReComTestRepository.GetReComTestByResouceId(id);
        }

        /// <summary>
        /// 获取自学任务单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private dynamic getTask(int id)
        {
            return ReTaskRepository.GetReTaskByResouceId(id);
        }
        /// <summary>
        /// 获取互动题板
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private List<dynamic> getWhiteboard(int id)
        {
            return ResourceWhiteboardRepository.GetResourceWhiteboardByResouceId(id).ToList();
        }
        /// <summary>
        /// 获取试题
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private dynamic getTest(int id)
        {
            return ReTestRepository.GetReTestByResouceId(id);
        }
        #endregion
    }
}
